package com.yn.common.utils.ex;

import com.alibaba.druid.wall.violation.ErrorCode;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

/**
 * @description:    消除不必要的if判断
 * @author: 叶甯
 * @create: 2022/10/13 16:39
 * @since: 1.0.0
 * @copyright (C), 2022, https://gitee.com/johnny .All rights reserved.
 */
@NoArgsConstructor
public class Assert {

    public static void isTrue(boolean expression, String message) {
        int code = BaseErrorEnum.ERROR.getCode();
        isTrue(expression,code,message);
    }
    public static void isTrue(boolean expression, BaseError err) {
        isTrue(expression,err.getCode(),err.getMessage());
    }

    /**
     *
     * @param expression Boolean表达式，true：表示预期错误没有发生，继续执行；false:发生预期错误，中断执行
     * @param code      错误码
     * @param message   错误信息
     */
    public static void isTrue(boolean expression, int code, String message) {
        if (!expression) {
            throw new BusinessException(code, message);
        }
    }
    /**
     * 多个参数都不为空的情况校验通过，否则抛出异常
     * @param message
     * @param params
     */
    public static void isNotNull(String message,Object... params){
        int length = params.length;
        if(length>0){
            for (int i = 0; i < length; i++) {
                isTrue(!ObjectUtils.isEmpty(params[i]),message);
            }
        }
    }
}